package com.wnxy.service.impl;

import com.wnxy.dao.IProductDao;
import com.wnxy.domain.Product;
import com.wnxy.service.IProductService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Optional;

/**
 * @Classname ProductServiceImpl
 * @Description :
 * @Date 2021/2/23 18:06
 * @Created by Yeheng
 */
@Service
public class ProductServiceImpl implements IProductService {
    @Autowired
    IProductDao productDao;

    @Override
    public Product findOne(Integer pid) {

        Optional<Product> id = productDao.findById(pid);

        if (!id.isPresent()){
            throw new RuntimeException("没有该商品");
        }
        return id.get();
    }

    @Override
    public void reduceStock(Integer pid, Integer num) {
        Optional<Product> id = productDao.findById(pid);

        if (!id.isPresent()){
            throw new RuntimeException("没有该商品");
        }
        Product product = id.get();
        product.setStock(product.getStock()-num);
        productDao.save(product);

    }

    @Override
    public List<Product> findAll() {
        return productDao.findAll();

    }
}
